package org.farng.mp3.object;

/**
 * DOCUMENT ME!
 *
 * @author Eric Farng
 * @version $Revision: 1.8 $
 */
public class ObjectBooleanByte extends AbstractMP3Object {
    /** DOCUMENT ME! */
    int bitPosition = -1;

    /**
     * Creates a new ObjectBooleanByte object.
     *
     * @param identifier DOCUMENT ME!
     * @param bitPosition DOCUMENT ME!
     *
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public ObjectBooleanByte(String identifier, int bitPosition) {
        if ((bitPosition < 0) || (bitPosition > 7)) {
            throw new IndexOutOfBoundsException("Bit position needs to be from 0 - 7 : " + bitPosition);
        }

        this.bitPosition = bitPosition;
        this.identifier  = identifier;
    }

    public ObjectBooleanByte(ObjectBooleanByte copyObject) {
        super(copyObject);
        this.bitPosition = copyObject.bitPosition;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getBitPosition() {
        return bitPosition;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        return 1;
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean equals(Object obj) {
        if ((obj instanceof ObjectBooleanByte) == false) {
            return false;
        }

        ObjectBooleanByte object = (ObjectBooleanByte) obj;

        if (this.bitPosition != object.bitPosition) {
            return false;
        }

        return super.equals(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param arr DOCUMENT ME!
     * @param offset DOCUMENT ME!
     *
     * @throws NullPointerException DOCUMENT ME!
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public void readByteArray(byte[] arr, int offset) {
        if (arr == null) {
            throw new NullPointerException("Byte array is null");
        }

        if ((offset < 0) || (offset >= arr.length)) {
            throw new IndexOutOfBoundsException("Offset to byte array is out of bounds: offset = " + offset + ", array.length = " + arr.length);
        }

        byte newValue = arr[offset];

        newValue >>= bitPosition;
        newValue &= 0x1;
        this.value = new Boolean(newValue == 1);
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        return "" + value;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public byte[] writeByteArray() {
        byte[] retValue;

        retValue = new byte[1];

        if (value != null) {
            retValue[0] = (byte) (((Boolean) value).booleanValue() ? 1 : 0);
            retValue[0] <<= bitPosition;
        }

        return retValue;
    }
}